package com.biz.service.basic;

import com.biz.model.Hmodel.TBizPerson;
import com.biz.model.Hmodel.TSysUser;
import com.biz.model.Pmodel.sys.Puser;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.model.Pager;
import com.framework.model.Paging;
import com.framework.utils.MD5;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tomchen on 17/1/11.
 */
@Service("bizpersonService")
public class BizPersonServiceImpl extends BaseServiceImpl<TBizPerson> implements BizpersonServiceI {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Autowired
    private BaseDaoI<TSysUser> tSysUserDao;

    @Override
    public Paging<Puser> queryBizpersons(Map map) throws Exception {
        /**
         * 封装查询参数
         */
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);

        Pager<Puser> pager_back = (Pager<Puser>) dao
                .findForPager1("BizpersonDao.findPage",
                        "BizpersonDao.count", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }

    @Override
    public TSysUser getTsysUserById(String id)
    {
        TSysUser tSysUser = tSysUserDao.getById(TSysUser.class, id);

        return tSysUser;
    }

    @Override
    public void addOrUpdateByPuser(Puser puser)throws  Exception{
        if(StringUtil.isNullOrEmpty(puser.getId())){//add
            TSysUser tSysUser = new TSysUser();
            BeanUtils.copyProperties(puser,tSysUser);
            tSysUser.setId(UuidUtil.get32UUID());
            tSysUser.setIdentity((short)3);
            tSysUser.setIsLock((short)0);
            tSysUser.setPwd(MD5.md5("123456"));//设置初始化密码
            tSysUser.setIsdel((short)0);
            tSysUser.setCreateTime(new Date());
            if(StringUtil.isNullOrEmpty(puser.getProvince())){
                tSysUser.setProvince("10");
            }
            if(StringUtil.isNullOrEmpty(puser.getCity())){
                tSysUser.setCity("171");
            }
            if(StringUtil.isNullOrEmpty(puser.getArea())){
                tSysUser.setArea("2112");
            }
            tSysUserDao.save(tSysUser);
            if(!testLoginName(puser.getLoginName(), ""))
            {
                throw new RuntimeException("登录名已存在，请尝试其他登录名");
            }
        }else{//update
            TSysUser tSysUser = tSysUserDao.getById(TSysUser.class,puser.getId());

            tSysUser.setMapId(puser.getMapId());
            tSysUser.setPersonName(puser.getPersonName());
            tSysUser.setCoverId(puser.getCoverId());
            tSysUser.setLoginName(puser.getLoginName());
            tSysUser.setPhone(puser.getPhone());
            tSysUser.setNotes(puser.getNotes());
            tSysUser.setProvince(puser.getProvince());
            tSysUser.setCity(puser.getCity());
            tSysUser.setArea(puser.getArea());
            tSysUser.setGroupName(puser.getGroupName());
            if(StringUtil.isNullOrEmpty(puser.getProvince())){
                tSysUser.setProvince("10");
            }
            if(StringUtil.isNullOrEmpty(puser.getCity())){
                tSysUser.setCity("171");
            }
            if(StringUtil.isNullOrEmpty(puser.getArea())){
                tSysUser.setArea("2112");
            }
            tSysUserDao.update(tSysUser);
            if(!testLoginName(puser.getLoginName(), puser.getId()))
            {
                throw new RuntimeException("登录名已存在，请尝试其他登录名");
            }
        }
    }


    private boolean testLoginName(String loginName, String userId){
        Map map = new HashMap();
        map.put("loginName",loginName);
        map.put("userId",userId);
        boolean flag = false; //已重复
        try {
            Integer count = (Integer)dao.findForObject("BizpersonDao.getCountByParam",map);
            flag = count.intValue() == 0;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return flag;
    }

    @Override
    public Paging<Puser> showBizMebList(Map map) throws Exception {
        /**
         * 封装查询参数
         */
        Pager<Map<String, Object>> pager = calculateAndTransformByMap(map);

        Pager<Puser> pager_back = (Pager<Puser>) dao
                .findForPager1("BizpersonDao.showBizMebList",
                        "BizpersonDao.showBizMebListCount", pager);
        //封装成适合前台使用的格式
        Paging paging = new Paging<>();
        paging.setRows(pager_back.getExhibitDatas());
        paging.setTotal((long) pager_back.getRecordCount());
        return paging;
    }
}
